//
//  BusinessViewController.m
//  We365
//
//  Created by Pham Khuong Duy on 12/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BusinessViewController.h"
#import "MapViewController.h"
#import "WebViewController.h"
#import "Bookmark.h"

@implementation BusinessViewController
@synthesize table;
@synthesize item;
@synthesize buttonCall;
@synthesize buttonWeb;
@synthesize buttonMap;
@synthesize buttonFavourite;


NSString *titleFavorite;
bool isAdd;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithItem:(Item *)i
{
    self = [super init];
    [self setItem:[i retain]];
    return self;
}

- (IBAction)makeCall:(id)sender {
    if (![item.Phone isEqualToString:@"N/A"]) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", item.Phone]]];
    }
}

- (IBAction)showWeb:(id)sender {
    if (![item.Website isEqualToString:@"N/A"]) {
    WebViewController *webViewController = [[WebViewController alloc] initWithWebsite:item.Website];
    [self.navigationController pushViewController:webViewController animated:YES];
    [webViewController release];
    }
}

- (IBAction)addFavourite:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSLog(@"button title %@", button.titleLabel.text);
    Bookmark *bookmark = [[Bookmark alloc] init];
    if(!isAdd)
    {
        [bookmark removeFavourite:item.ItemID];
        //[button setTitle:@"Add" forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:BGAddFavorite] forState:UIControlStateNormal];
        isAdd = TRUE;
    }
    else
    {
        [bookmark addFavourite:item];
        [button setTitle:@"Remove" forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:BGRemoveFavorite] forState:UIControlStateNormal];
        isAdd = FALSE;
    }
    [bookmark release];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([item.Address length] == 0) {
        item.Address = @"N/A";
    }
    
    if([item.Phone length] == 0) {
        item.Phone = @"N/A";
    }
    
    if([item.Website length] == 0) {
        item.Website = @"N/A";
        buttonWeb.enabled = NO;
    }
    
    table.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:BGTable]];
    table.separatorColor = [UIColor yellowColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:BGTable]];
    
    Bookmark *bookmark = [[Bookmark alloc] init];
    if([bookmark isFavourite:item.ItemID]) {
        //[self.buttonFavourite setTitle:@"Remove" forState:UIControlStateNormal];
        isAdd = FALSE;
        NSLog(@"Set Title Remove");
        titleFavorite = BGRemoveFavorite; 
    }
    else {
        //[self.buttonFavourite setTitle:@"Add" forState:UIControlStateNormal];
        isAdd = TRUE;
        NSLog(@"Set Title Add");
        titleFavorite = BGAddFavorite;
    }
    NSLog(@"Button Favorite Title: %@", self.buttonFavourite.titleLabel.text);
    [bookmark release];
    
    
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:BGShowMap] style:UIBarButtonItemStylePlain target:self action:@selector(showMap:)];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    [rightBarButton release];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setTable:nil];
    [self setButtonCall:nil];
    [self setButtonWeb:nil];
    [self setButtonMap:nil];
    [self setButtonFavourite:nil];
    [super viewDidUnload];
    [self setItem:nil];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc
{
    [item release];
    [table release];
    [buttonCall release];
    [buttonWeb release];
    [buttonMap release];
    [buttonFavourite release];
    [super dealloc];
}

- (IBAction)showMap:(id)sender
{
    if (![item.Address isEqualToString:@"N/A"]) {
    MapViewController *mapViewController = [[MapViewController alloc] initWithItem:item];
    [self.navigationController pushViewController:mapViewController animated:YES];
    [mapViewController release];
    }
}

- (NSString *)title
{
    return @"Information";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == 1)
        return 145;
    else if ([indexPath row] == 2)
        return 50;
    else
        return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    int row = [indexPath row];
    if(row == 0)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
        
        // Line Title
        buttonFavourite = [[UIButton alloc] initWithFrame:CGRectMake(260, 15, 32, 32)];
        [buttonFavourite setImage:[UIImage imageNamed:titleFavorite] forState:UIControlStateNormal];
        [buttonFavourite addTarget:self action:@selector(addFavourite:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:buttonFavourite];
        
        UILabel *name = [[[UILabel alloc] initWithFrame:CGRectMake(10, 5, 250, 50)] autorelease];
        name.textColor = [UIColor yellowColor];
        name.font = [UIFont boldSystemFontOfSize:22];
        name.textAlignment = UITextAlignmentCenter;
        name.backgroundColor = [UIColor clearColor];
        name.shadowColor = [UIColor blackColor];
        name.shadowOffset = CGSizeMake(1.0f, 1.0f);
        name.text = item.ItemName;
        [name setAdjustsFontSizeToFitWidth:YES];
        [cell.contentView addSubview:name];
    }
    else if(row == 1)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];

        // Line Address
        buttonMap = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 32, 32)];
        [buttonMap setImage:[UIImage imageNamed:BGAddress] forState:UIControlStateNormal];
        [buttonMap addTarget:self action:@selector(showMap:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:buttonMap];
        
        UILabel *address = [[[UILabel alloc] initWithFrame:CGRectMake(40, 10, 250, 40)] autorelease];
        address.textColor = [UIColor redColor];
        address.shadowColor = [UIColor blackColor];
        address.shadowOffset = CGSizeMake(0.3f, 0.3f);
        address.font = [UIFont boldSystemFontOfSize:14];
        address.backgroundColor = [UIColor clearColor];
        address.text = item.Address;
        address.lineBreakMode = UILineBreakModeWordWrap;
        address.numberOfLines = 0;
        [cell.contentView addSubview:address];
        
        // Line Phone
        buttonCall = [[UIButton alloc] initWithFrame:CGRectMake(5, 55, 32, 32)];
        [buttonCall setImage:[UIImage imageNamed:BGPhone] forState:UIControlStateNormal];
        [buttonCall addTarget:self action:@selector(makeCall:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:buttonCall];

        UILabel *phone = [[[UILabel alloc] initWithFrame:CGRectMake(40, 60, 250, 20)] autorelease];
        phone.font = [UIFont boldSystemFontOfSize:14];
        phone.textColor = [UIColor whiteColor];
        phone.shadowColor = [UIColor blackColor];
        phone.shadowOffset = CGSizeMake(0.3f, 0.3f);
        phone.backgroundColor = [UIColor clearColor];
        phone.text = item.Phone;
        [cell.contentView addSubview:phone];

        // Line Website
        buttonWeb = [[UIButton alloc] initWithFrame:CGRectMake(5, 100, 32, 32)];
        [buttonWeb setImage:[UIImage imageNamed:BGWeb] forState:UIControlStateNormal];
        [buttonWeb addTarget:self action:@selector(showWeb:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:buttonWeb];
                
        UILabel *website = [[[UILabel alloc] initWithFrame:CGRectMake(40, 105, 250, 20)] autorelease];
        website.font = [UIFont boldSystemFontOfSize:14];
        website.backgroundColor = [UIColor clearColor];
        website.textColor = [UIColor blueColor];
        website.shadowColor = [UIColor whiteColor];
        website.shadowOffset = CGSizeMake(0.3f, 0.3f);
        website.text = item.Website;
        [cell.contentView addSubview:website];
        
    }
    else if(row == 2)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
                 
        int imageIndex = (int)(item.Rating / 0.5);
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", imageIndex]];
        UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(60, 10, 180, 32)] autorelease];
        imageView.image = image;
        [cell.contentView addSubview:imageView]; 
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        cell.backgroundColor = [UIColor grayColor];
    }   
    else if (indexPath.row==1) {
        cell.backgroundColor = [UIColor lightGrayColor];
    }
    else {
        cell.backgroundColor = [UIColor whiteColor];
    }
}

@end
